package com.yhh.common.http;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * RestTemplate用于与外部系统请求工具类
 * @author liukai
 *
 */
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(SimpleClientHttpRequestFactory factory){
        RestTemplate restTemplate = new RestTemplate(factory);
        //默认编码集是ISO-8859-1
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.ISO_8859_1));
        return restTemplate;
    }
    @Bean
    public SimpleClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(20000);//请求超时时间单位为ms
        factory.setConnectTimeout(20000);//连接超时时间单位为ms
        return factory;
    }
}